﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;

namespace OT.Areas.Admin.Controllers
{
    public class HistoryController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, int searchString, int pgindex)
        {
            List<Histories> log = (from c in OTDb.Histories
                                   select new Histories
                                   {
                                       HistoryID = c.HistoryID,
                                       DateAdded = c.DateAdded.Value,
                                       Action = c.Action,
                                       Description = c.Description,
                                       UserID = c.UserID.Value,
                                       UserName = (OTDb.Users.Where(u => u.UserID == c.UserID.Value).FirstOrDefault().FirstName +" " + OTDb.Users.Where(u => u.UserID == c.UserID.Value).FirstOrDefault().LastName)
                                   }).OrderByDescending(o => o.DateAdded).ToList<Histories>();

            if (searchString!=0)
                log = log.Where(r => r.UserID.Equals(searchString)).ToList();
            int recount = log.Count;
            log = log.Skip<Histories>(pgindex * (pageNumber - 1)).Take<Histories>(pgindex).ToList<Histories>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = log,
                RecordCount = recount
            });
        }
        #endregion

        #region JsonCallExtara
        public JsonResult LoadUser()
        {
            var userlist = OTDb.Users.ToList();
            var userdata = userlist.Select(m => new SelectListItem()
            {
                Text = m.FirstName + " " + m.LastName,
                Value = m.UserID.ToString(),
            });
            return Json(userdata, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetHistoryDetail(int HistoryID)
        {
            var description = OTDb.Histories.Where(h => h.HistoryID.Equals(HistoryID)).FirstOrDefault().Description;
            return Json(description, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Delete
        public ActionResult Delete(int HistoryID)
        {
            Histories err = null;
            try
            {
                var categry = OTDb.Histories.Where(r => r.HistoryID == HistoryID).FirstOrDefault();
                if (categry != null)
                {
                    err = new Histories();
                    err.HistoryID = (int)categry.HistoryID;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(err);
        }

        [HttpPost]
        public string Delete(int HistoryID, string IsDelete)
        {
            string result = "";
            try
            {
                var categry = OTDb.Histories.Where(r => r.HistoryID == HistoryID).FirstOrDefault();
                if (categry != null)
                {
                    OTDb.Histories.Remove(categry);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}